package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bed")
@Api(tags = "床位管理相关接口")
public class BedController extends BaseController {

    @Autowired
    private BedService bedService;

    @GetMapping("/read/room/{roomId}")
    @ApiOperation(value = "根据房间id查询床位", notes = "传入房间id")
    public ResponseResult<List<BedVo>> readBedByRoomId(
            @ApiParam(value = "房间ID", required = true) @PathVariable("roomId") Long roomId) {
        List<BedVo> beds = bedService.getBedsByRoomId(roomId);
        return success(beds);
    }

    /**
     * 创建床位
     * @param dto
     * @return
     */
    @PostMapping("/create")
    public ResponseResult createBed(@RequestBody BedDto dto){
//        dto 转换为 entity
//        使用hutool工具包的属性拷贝
        Bed bed = BeanUtil.toBean(dto, Bed.class);
        bedService.createBed(bed);
        return success();
    }

    /**
     * 根据id查询床位
     * @param id
     * @return
     */
    @GetMapping("/read/{id}")
    public ResponseResult<BedVo> readBedById(@PathVariable(name = "id") Long id){
        BedVo bedVo = bedService.readBedById(id);
        return success(bedVo);
    }

    /**
     * 更新床位
     * @param dto
     * @return
     */
    @PutMapping("/update")
    public ResponseResult updateBedById(@RequestBody BedDto dto){
//        dto 转换为 entity
//        使用hutool工具包的属性拷贝
        Bed bed = BeanUtil.toBean(dto, Bed.class);
        bedService.updateBedById(bed);
        return success();
    }

    /**
     * 根据id删除床位
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteBedById(@PathVariable(name = "id") Long id){
        bedService.deleteBedById(id);
        return success();
    }
}
